﻿using FineUI;
using SysManage.BLL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZHDJ.Core.Util;
using ZHDJ.Data.Models.Center;
using ZHDJ.WebPage;

namespace SysManage.Module.Secretary
{
    public partial class PartySecretaryManageEdit : PageBase
    {
        #region ViewPower

        /// <summary>
        /// 本页面的浏览权限，空字符串表示本页面不受权限控制
        /// </summary>
        public override string ViewPower
        {
            get
            {
                return "";
            }
        }
        #endregion

        #region Page_Load

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
                LoadData();
            }
        }

        private void BindData()
        {
            ControlBusiness.ControlBindDataDictionary(ddlCategory, "ZHDJ.Secretary.Category");
        }

        private void LoadData()
        {
            btnClose.OnClientClick = ActiveWindow.GetHideReference();
            string id = GetQueryIntValue("id");
            if (id != "")
            {
                PartySecretary secretary = DB.PartySecretarys.Find(id);
                if (secretary == null)
                {
                    // 参数错误，首先弹出Alert对话框然后关闭弹出窗口
                    Alert.Show("参数错误！", String.Empty, ActiveWindow.GetHideReference());
                    return;
                }

                txtSecretaryName.Text = secretary.SecretaryName;
                txtDuties.Text = secretary.Duties;
                ddlCategory.SelectedValue = secretary.Category;
                rblIsVisible.SelectedValue = secretary.IsVisible.ToString();
                hdImageId.Text = secretary.ImageID;
                if (!string.IsNullOrEmpty(hdImageId.Text))
                {
                    filePhoto.EmptyText = UpFileBusiness.GetFileName(hdImageId.Text);
                    HyperLink1.ImageID = hdImageId.Text;
                }
            }
        }
        #endregion

        #region Events

        private void SaveData()
        {
            string id = GetQueryIntValue("id");
            string imageUrl = string.Empty;
            PartySecretary secretary;

            if (id != "")
            {
                //更新
                secretary = DB.PartySecretarys.Find(id);
                if (secretary == null)
                {
                    Alert.Show("参数错误！", String.Empty, ActiveWindow.GetHideReference());
                    return;
                }

                DB.SaveChanges();
            }
            else
            {
                secretary = new PartySecretary();
                secretary.ID = StringHelper.NewID;
                secretary.CreateTime = DateTime.Now;
                secretary.CreateBy = CurrentUser.LoginID;

                DB.PartySecretarys.Add(secretary);
            }

            secretary.SecretaryName = txtSecretaryName.Text.Trim();
            secretary.Duties = txtDuties.Text.Trim();
            secretary.Category = ddlCategory.SelectedValue;
            secretary.IsVisible = Convert.ToInt32(rblIsVisible.SelectedValue);
            secretary.ModifyTime = DateTime.Now;
            secretary.ModifyBy = CurrentUser.LoginID;

            if (!string.IsNullOrEmpty(hdImageId.Text))
            {
                secretary.ImageID = hdImageId.Text;
            }

            DB.SaveChanges();
        }

        /// <summary>
        /// 保存并关闭按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSaveClose_Click(object sender, EventArgs e)
        {
            SaveData();

            PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
        }

        protected void filePhoto_FileSelected(object sender, EventArgs e)
        {
            var rs = UpFileBusiness.Upload(filePhoto, CurrentUser.LoginID);
            if (rs.IsSuccess)
            {
                hdImageId.Text = rs.FileID;
                HyperLink1.ImageID = rs.FileID;
            }
        }
        #endregion
    }
}